/**
 ******************************************************************************
 * @file    can.h
 * @author  hyseim software Team
 * @date    12-Mar-2024
 * @brief   This file provides all the headers of the dmac functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_H__
#define __CAN_H__

#ifdef __cplusplus
extern "C"
{
#endif
/* Includes ------------------------------------------------------------------*/
#include "chip_define.h"
#include "typedefine.h"
#include "common.h"

/** @addtogroup im110_StdPeriph_Driver
 * @{
 */

/** @addtogroup CAN
 * @{
 */

/* Exported types ------------------------------------------------------------*/

#if 1
/* start input to im110gw.h*/
/* ================================================================================ */
/* ==============        Controller Area Network  (CAN)               ============= */
/* ================================================================================ */
typedef struct
{
    struct
    {
        __IO uint32_t Identifier;  /*!< message ID:standard identifier[10:0],  extended identifier[28:0],ESI[31]   Address offset: 0x0  */
        __IO uint8_t Control;      /*!< Control bits:IDE[7]/RTR[6]/FDF[5]/BRS[4]/DLC[3:0],                         Address offset: 0x4  */
        __IO uint8_t Status;       /*!< Control bits:KOER[7:5],TX[4]                                               Address offset: 0x5  */
        __IO uint16_t TTCAN;       /*!< TTCAN CYCLE_TIME                                                           Address offset: 0x6  */
        __IO uint8_t DataByte[64]; /*!< Payload bytes:CAN max=8, CAN FD max=64                                     Address offset: 0x8  */
        __I  uint32_t RTS_32bit;   /*!< Reception Time Stamp                                                       Address offset: 0x48 */
        __I  uint32_t RTS_64bit;   /*!< Reception Time Stamp                                                       Address offset: 0x4c */
    } RBUF;

    struct
    {
        __IO uint32_t Identifier;  /*!< message ID:standard identifier[10:0],  extended identifier[28:0],TTSEN[31] Address offset:0x50  */
        __IO uint32_t Control;      /*!< Control bits:IDE[7]/RTR[6]/FDF[5]/BRS[4]/DLC[3:0]                         Address offset:0x54  */
        __IO uint8_t DataByte[64]; /*!< Payload bytes:CAN max=8, CAN FD max=64                                  Address offset:0x58 */
        __I  uint32_t TTS_32bit;   /*!< Transmission Time Stamp                                                    Address offset: 0x98 */
        __I  uint32_t TTS_64bit;   /*!< Transmission Time Stamp                                                    Address offset: 0x9c */
    } TBUF;

    __IO uint8_t CFG_STAT;      /*configuration and status      Address offset:0xa0*/
    __IO uint8_t TCMD;          /*transmission command          Address offset:0xa1*/
    __IO uint8_t TCTRL;         /* transmission control         Address offset:0xa2*/
    __IO uint8_t RCTRL;         /* receiver control             Address offset:0xa3*/
    __IO uint8_t RTIE;          /* receive and transmit interrupt enable    Address offset:0xa4*/
    __IO uint8_t RTIF;          /*receive and transmit interrupt flag       Address offset:0xa5*/
    __IO uint8_t ERRINT;        /* error interrupt                          Address offset:0xa6*/
    __IO uint8_t LIMIT;         /*limits                                    Address offset:0xa7*/
    __IO uint8_t S_SEG_1;       /*(slow) bit timing segment 1 (7:0)         Address offset:0xa8*/
    __IO uint8_t S_SEG_2;       /* (slow) bit timing segment 2(6:0)         Address offset:0xa9*/
    __IO uint8_t S_SJW;         /* (slow) synchronisation jump width(6:0)   Address offset:0xaa*/
    __IO uint8_t S_PRESC;       /* (slow) prescaler (7:0)                   Address offset:0xab*/
    __IO uint8_t F_SEG_1;       /*(fast) bit timing segment 1 (4:0)         Address offset:0xac*/
    __IO uint8_t F_SEG_2;       /*(fast) bit timing segment 2 (3:0)         Address offset:0xad*/
    __IO uint8_t F_SJW;         /*(fast) synchronisation jump width (3:0)   Address offset:0xae*/
    __IO uint8_t F_PRESC;       /*(fast) prescaler (7:0)                    Address offset:0xaf*/
    __IO uint8_t EALCAP;        /* error and arbitration lost capture       Address offset:0xb0*/
    __IO uint8_t TDC;           /* transceiver delay compensation           Address offset:0xb1*/
    __IO uint8_t RECNT;         /* receive error counter                    Address offset:0xb2*/
    __IO uint8_t TECNT;         /* transmit error counter                   Address offset:0xb3*/
    __IO uint8_t ACFCTRL;       /* acceptance filter control                Address offset:0xb4*/
    __IO uint8_t TIMECFG;       /* CiA 603 time-stamping configuration      Address offset:0xb5*/
    __IO uint16_t ACF_EN_0_1;      /* acceptance filter enable register 0      Address offset:0xb6*/
    __IO uint32_t ACF_0_3;      /* acceptance filter register 0-3           Address offset:0xb8*/ 
    __IO uint8_t VER_0;         /* minor version number                     Address offset:0xbc*/
    __IO uint8_t VER_1;         /* major version number                     Address offset:0xbd*/
    __IO uint8_t TBSLOT;        /* TB slot pointer                          Address offset:0xbe*/
    __IO uint8_t TTCFG;         /* TTCAN: configuration                     Address offset:0xbf*/
    __IO uint8_t REF_MSG_0;     /* TTCAN: reference message                 Address offset:0xc0*/
    __IO uint8_t REF_MSG_1;     /* TTCAN: reference message                 Address offset:0xc1*/
    __IO uint8_t REF_MSG_2;     /* TTCAN: reference message                 Address offset:0xc2*/
    __IO uint8_t REF_MSG_3;     /* TTCAN: reference message                 Address offset:0xc3*/
    __IO uint8_t TRIG_CFG_0;    /* TTCAN: trigger configuration 0           Address offset:0xc4*/
    __IO uint8_t TRIG_CFG_1;    /* TTCAN: trigger configuration 1           Address offset:0xc5*/
    __IO uint8_t TT_TRIG_0;     /* TTCAN: trigger time                      Address offset:0xc6*/
    __IO uint8_t TT_TRIG_1;     /* TTCAN: trigger time                      Address offset:0xc7*/
    __IO uint8_t TT_WTRIG_0;    /* TTCAN: watch trigger time                Address offset:0xc8*/
    __IO uint8_t TT_WTRIG_1;    /* TTCAN: watch trigger time                Address offset:0xc9*/
} CAN_t;
/*end input to im110gw.h*/
#endif

/*------------------------------------------------------------------------------------------------------*/
/*---                             Controller Area Network  (CAN)                                     ---*/
/*------------------------------------------------------------------------------------------------------*/
/*******************************  Bit definition for CAN register  *******************************/
#define CAN_RESET           (0x1 << 7)
#define CAN_LBME            (0x1 << 6)
#define CAN_LBMI            (0x1 << 5)
#define CAN_TPSS            (0x1 << 4)  /*Transmission Primary Single Shot mode for PTB*/
#define CAN_TSSS            (0x1 << 3)  /*Transmission Secondary Single Shot mode for STB*/
#define CAN_RACTIVE         (0x1 << 2)
#define CAN_TACTIVE         (0x1 << 1)
#define CAN_BUSOFF          (0x1 << 0)

#define CAN_TBSEL           (0x1 << 7)  /*Transmit Buffer Select*/
#define CAN_LOM             (0x1 << 6)
#define CAN_STBY            (0x1 << 5)
#define CAN_TPE             (0x1 << 4)
#define CAN_TPA             (0x1 << 3)
#define CAN_TSONE           (0x1 << 2)
#define CAN_TSALL           (0x1 << 1)
#define CAN_TSA             (0x1 << 0)

#define CAN_FD_ISO          (0x1 << 7)  /*CAN FD ISO mode*/
#define CAN_TSNEXT          (0x1 << 6)  /*Transmit buffer Secondary NEXT*/
#define CAN_TSMODE          (0x1 << 5)  /*Transmit buffer Secondary operation MODE*/
#define CAN_TTTBM           (0x1 << 4)
#define CAN_TSSTAT          (0x1 << 0)

#define CAN_SACK            (0x1 << 7)
#define CAN_ROM             (0x1 << 6)  /*Receive buffer Overflow Mode*/
#define CAN_ROV             (0x1 << 5)
#define CAN_RREL            (0x1 << 4)
#define CAN_RBALL           (0x1 << 3)
#define CAN_RSTAT           (0x1 << 0)

#define CAN_RIE             (0x1 << 7)  /*Receive Interrupt Enable*/
#define CAN_ROIE            (0x1 << 6)  /*RB Overrun Interrupt Enable*/
#define CAN_RFIE            (0x1 << 5)  /*RB Full Interrupt Enable*/
#define CAN_RAFIE           (0x1 << 4)  /*RB Almost Full Interrupt Enable*/
#define CAN_TPIE            (0x1 << 3)  /*Transmission Primary Interrupt Enable*/
#define CAN_TSIE            (0x1 << 2)  /*Transmission Secondary Interrupt Enable*/
#define CAN_EIE             (0x1 << 1)  /*Error Interrupt Enable*/
#define CAN_TSFF            (0x1 << 0)  /*Transmit Secondary buffer Full Flag*/

#define CAN_RIF             (0x1 << 7)
#define CAN_ROIF            (0x1 << 6)
#define CAN_RFIF            (0x1 << 5)
#define CAN_RAFIF           (0x1 << 4)
#define CAN_TPIF            (0x1 << 3)
#define CAN_TSIF            (0x1 << 2)
#define CAN_EIF             (0x1 << 1)
#define CAN_AIF             (0x1 << 0)

#define CAN_EWARN           (0x1 << 7)  /*Error WARNing limit reached*/
#define CAN_EPASS           (0x1 << 6)  /*Error Passive mode active*/
#define CAN_EPIE            (0x1 << 5)  /*Error Passive Interrupt Enable*/
#define CAN_EPIF            (0x1 << 4)  /*Error Passive Interrupt Flag*/
#define CAN_ALIE            (0x1 << 3)  /*Arbitration Lost Interrupt Enable*/
#define CAN_ALIF            (0x1 << 2)  /*Arbitration Lost Interrupt Flag*/
#define CAN_BEIE            (0x1 << 1)  /*Bus Error Interrupt Enable*/
#define CAN_BEIF            (0x1 << 0)  /*Bus Error Interrupt Flag*/

#define CAN_ALC_POS_EALCAP      (0x1 << 0)
#define CAN_KOER_POS_EALCAP     (0x1 << 5)

#define CAN_SEL_MASK            (0x1 << 5)
#define CAN_ACFADR              (0x1 << 0)

#define CAN_TTEN                (0x1 << 0)  /*Time Trigger Enable*/



/** 
  * @brief  CAN PIN Init Structure definition  
  */
typedef struct
{
    char *CAN_TXD_Pin0; /*!< Specifies the setting pin TXD */
    char *CAN_RXD_Pin1; /*!< Specifies the setting pin RXD */
    char *CAN_STB_Pin2; /*!< Specifies the setting pin STB */
} CAN_PIN_Init_t;

/**
 * @brief  CAN SlowBaudRate structure definition
 */
typedef struct
{
    uint8_t CAN_S_seg1; /*!< Specifies the number of time quanta in Bit Segment 1. 
                            This parameter can be a value of @ref CAN_Recommendations_slowbaud_can_clk  */
    uint8_t CAN_S_seg2; /*!< Specifies the number of time quanta in Bit Segment 2.
                            This parameter can be a value of @ref CAN_Recommendations_slowbaud_can_clk  */
    uint8_t CAN_S_SJW;  /*!< Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or
                            shorten a bit to perform resynchronization.
                            This parameter can be a value of @ref CAN_Recommendations_slowbaud_can_clk  */
    uint8_t CAN_S_Prescaler; /*!< Specifies The prescaler divides CAN_Recommendations_slowbaud_can_clk.
                                It ranges from 0x0 to 0xff. */
} CAN_SlowBaudRate_t;

/**
 * @brief  CAN FD FastBaudRate structure definition
 */
typedef struct
{
    uint8_t CAN_F_seg1; /*!< Specifies the number of time quanta in Bit Segment 1. 
                            This parameter can be a value of @ref CAN_Recommendations_fastbaud_can_clk */
    uint8_t CAN_F_seg2; /*!< Specifies the number of time quanta in Bit Segment 2.
                            This parameter can be a value of @ref CAN_Recommendations_fastbaud_can_clk */
    uint8_t CAN_F_SJW; /*!< Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or
                            shorten a bit to perform resynchronization.
                            This parameter can be a value of @ref CAN_Recommendations_fastbaud_can_clk */
    uint8_t CAN_F_Prescaler; /*!< Specifies The prescaler divides CAN_Recommendations_fastbaud_can_clk.
                                It ranges from 0 to 0xff. */
} CAN_FastBaudRate_t;

/**
 * @brief  CAN Transmitter Delay Compensation init structure definition
 */
typedef struct
{
    uint8_t CAN_TDCEN;
    uint8_t CAN_SSPOFF;     /*!< Specifies the number of time quanta in Bit Segment 1. 
                                This parameter can be a value of @ref CAN_TransmitterDelayCompensation */
} CAN_TransmitterDelayCompensation_t;

/**
 * @brief  CAN filter init structure definition
 */
typedef struct
{
    uint32_t CAN_Acode;     /*!< Acceptance CODE:ACC bit value to compare with ID bit of the received message */
    uint32_t CAN_Amask;     /*!< Acceptance MASK:acceptance check for these bits of receive identifier: 1-disabled,0-enable */
    uint16_t CAN_FilterChannel;/*!< Filter channel number of @ref CAN_FilterChannel */
    uint32_t CAN_FilterIdMode;  /*!< accept only either standard or extended frames or both frame types. 
                                  This parameter can be a value of @ref CAN_FilterIdMode */
} CAN_Filter_t;

/**
 * @brief  CAN_FD additional init structure definition
 */
typedef struct
{
    FunctionalState_t CAN_FD_ISO_Mode;           /*!< CAN FD ISO mode: 1-ISO CAN FD mode (ISO 11898-1:2015), 
                                                            0-Bosch CAN FD (non-ISO) mode */
    FunctionalState_t CAN_FD_FastBaudRate_Enable;/*!< Enable or disable switch to data / fast bit rate for the data payload and the CRC.
                                                        This parameter can be set either to ENABLE or DISABLE. */
    CAN_TransmitterDelayCompensation_t TransmitterDelayCompensation;/*!< reference CAN_TransmitterDelayCompensation_t*/
    CAN_FastBaudRate_t FastBaudRate;                                /*!< reference CAN_FastBaudRate_t*/
} CAN_FD_AddInit_t;

/**
 * @brief  CAN init structure definition
 */
typedef struct
{
    uint8_t  CAN_Specification;         /*!< Supports CAN specification. 
                                        This parameter can be a value of @ref CAN_Specification */
    CAN_SlowBaudRate_t SlowBaudRate;    /*!< reference CAN_SlowBaudRate_t*/
    CAN_FD_AddInit_t CAN_FD_AddInitStruct;  /*!< reference CAN_FD_AddInit_t*/
    FunctionalState_t CAN_Transmit_STB_FIFO_MODE;  /*!< Enable or disable the Transmit STB operation FIFO MODE. 
                                                        This parameter can be set either to ENABLE or DISABLE. */
    FunctionalState_t CAN_Retransmission;           /*!< Enable or disable the non-automatic retransmission mode.
                                                        This parameter can be set either to ENABLE or DISABLE. */
    FunctionalState_t CAN_ReceiveLocked;            /*!< Enable or disable the Receive FIFO Locked mode.
                                                        This parameter can be set either to ENABLE or DISABLE. */
    FunctionalState_t CAN_TimeTriggerEnable;        /*!< Enable or disable the time triggered communication mode.
                                                        This parameter can be set either to ENABLE or DISABLE. */
    uint8_t CAN_OperatingMode;                      /*!< Specifies the CAN operating mode.
                                                        This parameter can be a value of @ref CAN_operating_mode */
} CAN_Init_t;


/**
 * @brief  CAN Tx message structure definition
 */
typedef struct
{
    uint32_t StdId; /*!< Specifies the standard identifier.
                            This parameter can be a value between 0 to 0x7FF. */
    uint32_t ExtId; /*!< Specifies the extended identifier.
                            This parameter can be a value between 0 to 0x1FFFFFFF. */
    uint8_t IDE;    /*!< Specifies the type of identifier for the message that
                            will be transmitted. This parameter can be a value of @ref CAN_identifier_type */
    uint8_t RTR;    /*!< Specifies the type of frame for the message that will
                            be transmitted. This parameter can be a value of @ref CAN_remote_transmission_request */
    uint8_t FDF;    /*!< Specifies the type of frame for the message that will
                            be transmitted. This parameter can be a value of @ref CAN_Specification */
    uint8_t BRS;    /*!< Specifies the bit rate of data for the message that will
                            be transmitted. This parameter can be a value of @ref CAN_Bit_Rate_Switch */
    uint8_t DLC;    /*!< Specifies the length of the frame that will be
                            transmitted. This parameter can be a value of @ref CAN_Definition_of_the_DLC */
    uint8_t Data[64];   /*!< Contains the data to be transmitted. It ranges from 0 to 0xFF. */
} CAN_TxMsg;

/**
 * @brief  CAN Rx message structure definition
 */
typedef struct
{
    uint32_t StdId;   /*!< Specifies the standard identifier.
                            This parameter can be a value between 0 to 0x7FF. */
    uint32_t ExtId;   /*!< Specifies the extended identifier.
                            This parameter can be a value between 0 to 0x1FFFFFFF. */
    uint8_t ESI;      /*!< Specifies This is a read-only status bit for RBUF and is not available in TBUF.
                            This parameter can be a value:0-CAN node is error active,1-CAN node is error passive */
    uint8_t IDE;      /*!< Specifies the type of identifier for the message that
                            will be transmitted. This parameter can be a value of @ref CAN_identifier_type */
    uint8_t RTR;      /*!< Specifies the type of frame for the message that will
                            be transmitted. This parameter can be a value of
                            @ref CAN_remote_transmission_request */
    uint8_t FDF;      /*!< Specifies the type of frame for the message that will
                            be transmitted. This parameter can be a value of
                            @ref CAN_Specification */
    uint8_t BRS;      /*!< Specifies the bit rate of data for the message that will
                            be transmitted. This parameter can be a value of
                            @ref CAN_Bit_Rate_Switch */
    uint8_t DLC;      /*!< Specifies the length of the frame that will be
                            transmitted. This parameter can be a value of
                            @ref CAN_Definition_of_the_DLC */
    uint8_t KOER;     /*!< Specifies KOER in RBUF has the same meaning as the bits KOER in register EALCAP. 
                            KOER in RBUF becomes meaningful if RBALL=1 This parameter can be a value of
                            @ref CAN_Kind_Of_Error */        
    uint8_t TX;       /*!< Specifies Status bit TX in RBUF is set to 1 if the loop back mode  is activated 
                            and the core has received its own transmitted frame. */ 
    uint16_t CYCLE_TIME;/*!< Specifies The time-stamp CYCLE_TIME will be stored in RBUF only in TTCAN mode. 
                            This is the cycle time at the SOF of this frame. The cycle time of a reference message is always 0. */ 
    uint8_t Data[64];   /*!< Contains the data to be received. It ranges from 0
                            to 0xFF. */
    uint32_t rts;
} CAN_RxMsg;


/* Exported constants --------------------------------------------------------*/

/** @defgroup CAN_Exported_Constants
 * @{
 */

#define CAN_STB_MAX_NUM 16
#define CAN_RBUF_MAX_NUM 32

/** @defgroup CAN_operating_mode 
 * @{
 */
#define CAN_NORMAL_COMMUNICATE    ((uint8_t)0x0) /*!< Normal Communication Mode */
#define CAN_LOOPBACK_INTERNAL     ((uint8_t)0x1) /*!< Internal Loopback Mode */
#define CAN_LOOPBACK_EXTERNAL     ((uint8_t)0x2) /*!< External Loopback Mode */
#define CAN_LISTENONLY            ((uint8_t)0x3) /*!< Listen Only Mode */
#define CAN_LISTENONLY_LBME       ((uint8_t)0x4)  /*!< External Loopback combined with Listen Only Mode */
/**
  * @}
  */


/** @defgroup CAN_identifier_type
 * @{
 */
#define CAN_IDE_Id_Standard     ((uint8_t)0x0 << 7)   /*!< Standard Id */
#define CAN_IDE_Id_Extended     ((uint8_t)0x1 << 7)   /*!< Extended Id */
/**
  * @}
  */


/** @defgroup CAN_remote_transmission_request
 * @{
 */
#define CAN_RTR_Data                ((uint8_t)0x0 << 6)          /*!< Data frame */
#define CAN_RTR_Remote              ((uint8_t)0x1 << 6)          /*!< Remote frame */
/**
  * @}
  */


/** @defgroup CAN_Specification
 * @{
 */
#define CAN_FDF_2_Frame             ((uint8_t)0x0 << 5)   /*!< CAN frame 2.0A&B */
#define CAN_FDF_FD_Frame            ((uint8_t)0x1 << 5)   /*!< CAN_FD frame */
/**
  * @}
  */


/** @defgroup CAN_Bit_Rate_Switch
 * @{
 */
#define CAN_BRS_Disable             ((uint8_t)0x0 << 4)        /*!< nominal / slow bit rate for the complete frame */
#define CAN_BRS_Enable              ((uint8_t)0x1 << 4)        /*!< switch to data / fast bit rate for the data payload and the CRC */
/**
  * @}
  */


/** @defgroup CAN_FilterIdMode 
 * @{
 */
#define CAN_ONLY_STANDAR_ID          ((uint32_t)0x2 << 29) /*!< acceptance filter accepts only standard frames */
#define CAN_ONLY_EXTENDED_ID         ((uint32_t)0x3 << 29) /*!< acceptance filter accepts only extended frames */
#define CAN_BOTH_STANDAR_EXTENDED    ((uint32_t)0x0 << 29) /*!< acceptance filter accepts both standard or extended frames */
/**
  * @}
  */


/** @defgroup CAN_FilterChannel 
 * @{
 */
#define CAN_FILTER_CH0          ((uint8_t)0 ) /*!< acceptance filter channel 0 */
#define CAN_FILTER_CH1          ((uint8_t)1 ) /*!< acceptance filter channel 1 */
#define CAN_FILTER_CH2          ((uint8_t)2 ) /*!< acceptance filter channel 2 */
#define CAN_FILTER_CH3          ((uint8_t)3 ) /*!< acceptance filter channel 3 */
#define CAN_FILTER_CH4          ((uint8_t)4 ) /*!< acceptance filter channel 4 */
#define CAN_FILTER_CH5          ((uint8_t)5 ) /*!< acceptance filter channel 5 */
#define CAN_FILTER_CH6          ((uint8_t)6 ) /*!< acceptance filter channel 6 */
#define CAN_FILTER_CH7          ((uint8_t)7 ) /*!< acceptance filter channel 7 */
#define CAN_FILTER_CH8          ((uint8_t)8 ) /*!< acceptance filter channel 8 */
#define CAN_FILTER_CH9          ((uint8_t)9 ) /*!< acceptance filter channel 9 */
#define CAN_FILTER_CH10         ((uint8_t)10) /*!< acceptance filter channel 10 */
#define CAN_FILTER_CH11         ((uint8_t)11) /*!< acceptance filter channel 11 */
#define CAN_FILTER_CH12         ((uint8_t)12) /*!< acceptance filter channel 12 */
#define CAN_FILTER_CH13         ((uint8_t)13) /*!< acceptance filter channel 13 */
#define CAN_FILTER_CH14         ((uint8_t)14) /*!< acceptance filter channel 14 */
#define CAN_FILTER_CH15         ((uint8_t)15) /*!< acceptance filter channel 15 */
/**
  * @}
  */



/** @defgroup CAN_Recommendations_slowbaud_can_clk=40MHz 
  * @{
  */
#define S_seg1_250K             ((uint8_t)62)
#define S_seg2_250K             ((uint8_t)15)
#define  S_SJW_250K             ((uint8_t)15)
#define S_Prescaler_250K        ((uint8_t)1)

#define S_seg1_500K             ((uint8_t)30)
#define S_seg2_500K             ((uint8_t)7)
#define  S_SJW_500K             ((uint8_t)7)
#define S_Prescaler_500K        ((uint8_t)1)

#define S_seg1_1M               ((uint8_t)30)
#define S_seg2_1M               ((uint8_t)7)
#define  S_SJW_1M               ((uint8_t)7)
#define S_Prescaler_1M          ((uint8_t)0)
/**
  * @}
  */


/** @defgroup CAN_Recommendations_fastbaud_can_clk=40MHz 
  * @{
  */
#define F_seg1_1M               ((uint8_t)30)
#define F_seg2_1M               ((uint8_t)7)
#define  F_SJW_1M               ((uint8_t)7)
#define F_Prescaler_1M          ((uint8_t)0)

#define F_seg1_2M               ((uint8_t)14)
#define F_seg2_2M               ((uint8_t)3)
#define   F_SJW_2M              ((uint8_t)3)
#define F_Prescaler_2M          ((uint8_t)0)

#define F_seg1_4M               ((uint8_t)6)             
#define F_seg2_4M               ((uint8_t)1)
#define   F_SJW_4M              ((uint8_t)1)
#define F_Prescaler_4M          ((uint8_t)0)

#define F_seg1_5M               ((uint8_t)4)
#define F_seg2_5M               ((uint8_t)1)
#define   F_SJW_5M              ((uint8_t)1)
#define F_Prescaler_5M          ((uint8_t)0)

#define F_seg1_6667K            ((uint8_t)3)
#define F_seg2_6667K            ((uint8_t)0)
#define   F_SJW_6667K           ((uint8_t)0)
#define F_Prescaler_6667K       ((uint8_t)0)

#define F_seg1_8M               ((uint8_t)2)
#define F_seg2_8M               ((uint8_t)0)
#define   F_SJW_8M              ((uint8_t)0)
#define F_Prescaler_8M          ((uint8_t)0)
/**
  * @}
  */


/** @defgroup CAN_TransmitterDelayCompensation
  * @{
  */
#define CAN_TDC_Enable          ((uint8_t)0x1 << 7) /*Transmitter Delay Compensation ENable*/
#define TDC_SSPOFF_1M           ((uint8_t)32)       /*Secondary Sample Point OFFset*/
#define TDC_SSPOFF_2M           ((uint8_t)16)
#define TDC_SSPOFF_4M           ((uint8_t)8)
#define TDC_SSPOFF_5M           ((uint8_t)6)
#define TDC_SSPOFF_6667K        ((uint8_t)5)
#define TDC_SSPOFF_8M           ((uint8_t)4)
/**
  * @}
  */


/** @defgroup   
 *      \the ID of Frame is used for testing
 */	
#define  TEST_FRAME_ID     0x1554F789
/**
  * @}
  */


/** @defgroup CAN_Definition_of_the_DLC
  * @{
  */
#define DLC_0                   ((uint8_t)0x0)
#define DLC_1                   ((uint8_t)0x1)
#define DLC_2                   ((uint8_t)0x2)
#define DLC_3                   ((uint8_t)0x3)
#define DLC_4                   ((uint8_t)0x4)
#define DLC_5                   ((uint8_t)0x5)
#define DLC_6                   ((uint8_t)0x6)
#define DLC_7                   ((uint8_t)0x7)
#define DLC_8                   ((uint8_t)0x8)
#define DLC_12                  ((uint8_t)0x9)
#define DLC_16                  ((uint8_t)0xa)
#define DLC_20                  ((uint8_t)0xb)
#define DLC_24                  ((uint8_t)0xc)
#define DLC_32                  ((uint8_t)0xd)
#define DLC_48                  ((uint8_t)0xe)
#define DLC_64                  ((uint8_t)0xf)
/**
  * @}
  */


/** @defgroup CAN_Kind_Of_Error
  * @{
  */
#define NO_ERROR                ((uint8_t)0)
#define BIT_ERROR               ((uint8_t)1)
#define FORM_ERROR              ((uint8_t)2)
#define STUFF_ERROR             ((uint8_t)3)
#define ACK_ERROR               ((uint8_t)4)
#define CRC_ERROR               ((uint8_t)5)
#define OTHER_ERROR             ((uint8_t)6)
/**
  * @}
  */


/** @defgroup DMAC_DEBUG Macro
 * @{
 */
#define DEBUG 0
/**
 * @}
 */

/**
 * @}
 */

/* Exported macro ------------------------------------------------------------*/
#define CAN0 ((CAN_t *)(CAN0_BASE))
#define CAN1 ((CAN_t *)(CAN1_BASE))
#define CAN2 ((CAN_t *)(CAN2_BASE))
#if DEBUG
#define CAN_PRINT(format, ...) printf("[CAN]Line:%d,Function:%s-->" format "", __LINE__, __func__, ##__VA_ARGS__)
#else
#define CAN_PRINT(format, ...)
#endif
/* Exported functions ------------------------------------------------------- */
/* Initialization and Configuration functions *********************************/ 
void CAN_StructInit(CAN_Init_t *CAN_InitStruct);
void CAN_FD_AddStructInit(CAN_FD_AddInit_t *CAN_FD_AddStructInit);
void CAN_UnlockReg(CAN_t *CANx);
void CAN_LockReg(CAN_t *CANx);
void CAN_FD_AddInit(CAN_t *CANx, CAN_FD_AddInit_t *CAN_FD_AddStructInit);
void CAN_FilterChannelInit(CAN_t *CANx, CAN_Filter_t *CAN_FilterInitStruct);
void CAN_FilterDisalbeDefaultChannel_0(CAN_t *CANx);
void CAN_Init(CAN_t *CANx, CAN_Init_t *CAN_InitStruct);

/* CAN Frames Transmission and Reception functions ******************************************/
void CAN_TransmitViaPTB(CAN_t *CANx, CAN_TxMsg *txMessage);
void CAN_TransmitViaSTB(CAN_t *CANx, CAN_TxMsg *txMessage, uint8_t txMessagNum);
void CAN_TransmitOneFrameSTB(CAN_t *CANx);
void CAN_TransmitAllFrameSTB(CAN_t *CANx);
void CAN_ReceiveMsg(CAN_t *CANx, CAN_RxMsg *rxMessage);

/* Interrupts and flags management functions **********************************/
void CAN_RTIE_ITConfig(CAN_t *CANx, uint8_t CAN_IT, FunctionalState_t NewState);
void CAN_ERR_ITConfig(CAN_t *CANx, uint8_t CAN_IT, FunctionalState_t NewState);
FlagStatus_t CAN_RTIF_GetFlagStatus(CAN_t *CANx, uint32_t CAN_FLAG);
FlagStatus_t CAN_ERR_GetFlagStatus(CAN_t *CANx, uint32_t CAN_FLAG);
void CAN_RTIF_ClearFlag(CAN_t *CANx, uint32_t CAN_FLAG);
void CAN_ERRIF_ClearFlag(CAN_t *CANx, uint32_t CAN_FLAG);

void CAN_Reset(void *CAN_base);

/**
 * @}
 */

/**
 * @}
 */

#ifdef __cplusplus
}
#endif
#endif
/*******************(C)COPYRIGHT 2020 Hyseim ***********END OF FILE************/

